<?php

namespace App\Http\Model;

/**
 * @desc 校验数据类型
 * Class FormatCheck
 * @package app\http\model
 */
class FormatCheck
{
    const POST_URL = "https://api.threathunter.cn/zhbpro/phone_no_check";
    const SN_USER = "EtYheO4qy9u49R2z";
    const SN_KEY = "kKT5wlU2_DTO+QZ0iLP^~OzWOe3U4:m!";

    //手机号
    static function checkPhone($needle, $isCheckMobile = false)
    {
        if(empty($needle)) {
            return false;
        }
        $search = '/^0?[1|3][3|4|5|6|7|8|9][0-9]\d{8}$/';
        if (preg_match($search, $needle)) {
            return true;
        } else {
            return false;
        }
        
    }
    //验证码
    static function checkSmsCode($needle = '',$fixLen = 6){
        if (!$needle)
            return false;
        if (!is_int($needle))
            return false;
        if (strlen($needle) != $fixLen)
            return false;
        return true;
    }
    //时间戳
    static function checkTimeStamp($needle = ''){
        if(!$needle)
            return false;
        if(strtotime(date('Y-m-d H:i:s', $needle)) == $needle) {
            return true;
        } else {
            return false;
        }
    }
    //int
    static function checkInt($needle = '', $min = 0, $max = 1000000 )
    {
        if (!$needle)
            return false;
        if (!is_int($needle))
            return false;
        if ((strlen($needle) < $min) || (strlen($needle) > $max))
            return false;
        return true;
    }
    
    //string
    static function checkString($needle = '', $min = 0, $max = 1000000)
    {
        if (!$needle)
            return false;
        if (!is_string($needle))
            return false;
        if ((strlen($needle) < $min) || (strlen($needle) > $max))
            return false;
        return true;
    }
    
    //密码
    static function checkPassword($needle = '',$min = 6, $max = 16){
        if(!strlen($needle)){
            return false;
        }
        if(strlen($needle) < $min || (strlen($needle) > $max)){
            return false;
        }
        if(!preg_match("/^[a-z\d]*$/i",$needle))
        {
            return false;//有数字有字母 ";
        }
        return true;
    }
    
    //判断网址
    static function checkUrl($needle = '', $min = 1){
        if(!strlen($needle)){
            return false;
        }
        if(strlen($needle) < $min){
            return false;
        }
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%
=~_|]/i",$needle)) {
            return false;
        }
        return true;
        
    }


    static function aes256cfb_encrypt($phoneno) {
        $snkey = self::SN_KEY;
        $remainder = strlen($phoneno) % 16;
        if ($remainder) {
            $plain_text = $phoneno . str_repeat('\0', 16 - $remainder);
        } else {
            $plain_text = $phoneno;
        }
        $iv = openssl_random_pseudo_bytes(16);
        $value = openssl_encrypt($plain_text, 'AES-256-CFB', $snkey, OPENSSL_RAW_DATA, $iv);
        $cipher_text = base64_encode($iv . substr($value, 0, strlen($phoneno)));
        return $cipher_text;
    }


    static function aes256cfb_decrypt($phoneno) {
        $snkey = self::SN_KEY;
        $data = base64_decode($phoneno);
        $cipher_text = substr($data, 16, strlen($data));
        $remainder = strlen($cipher_text) % 16;
        if ($remainder) {
            $cipher = $cipher_text . str_repeat('\0', 16 - $remainder);
        } else {
            $cipher = $cipher_text;
        }
        $iv = substr($data, 0, 16);
        $plain_text = openssl_decrypt($cipher, 'AES-256-CFB', $snkey, OPENSSL_RAW_DATA, $iv);
        return substr($plain_text, 0, strlen($cipher_text));
    }

    //检测虚拟手机号static
    static function checkFalseMobile($mobileList) {
        $snuser = self::SN_USER;
        $post_url = self::POST_URL;

        $users = array();
        foreach ($mobileList as &$user) {
            array_push($users, array('user'=>sha1($user)));
        }

        $pstr = json_encode($users);
        $cstr = self::aes256cfb_encrypt($pstr);
        $payload = json_encode(array('snuser'=>$snuser, 'data'=>$cstr), JSON_UNESCAPED_SLASHES);

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $post_url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $payload,
            CURLOPT_HTTPHEADER => array(
                "Content-Type: application/json",
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        $res = json_decode($response, true);
        if ($res["status"] == 200){
            $result = json_decode(self::aes256cfb_decrypt($res["data"]), true);
            foreach ($result as $key => $value) {
                if ($value['risk'] != 0) {
                    return false;
                }
            }

            return true;
        }
        else {
            return false;
        }
    }

    public static function validPassword(string $password): bool {
        return preg_match('/(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)(?![!@#$%^&*_\-\+\=]+$)[\da-zA-z!@#$%^&*_\-\+\=]{8,}$/', $password) > 0;
    }
    
}
